package org.goj.user.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.goj.common.constant.ResultCode;
import org.goj.common.entity.user.Team;
import org.goj.common.entity.user.TeamData;
import org.goj.common.model.Result;
import org.goj.user.service.TeamDataService;
import org.goj.user.service.TeamService;
import org.goj.user.service.TeamUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 团队数据 前端控制器
 * </p>
 *
 * @author CK
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/team-data")
public class TeamDataController {

    @Resource
    private TeamDataService teamDataService;

    @Resource
    private TeamUserService teamUserService;

    @Resource
    private TeamService teamService;

    /**
     * 管理员添加或更新团队数据
     *
     * @param teamId      团队ID
     * @param userId      管理员ID
     * @param description 数据内容
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/7 20:20
     */
    @PostMapping("/insertTeamData")
    public Result<Boolean> insertTeamData(
            @RequestParam(value = "teamId") Integer teamId,
            @RequestParam(value = "userId") Integer userId,
            @RequestParam(value = "description") String description,
            @RequestParam(value = "teamDataId", required = false, defaultValue = "-1") Integer teamDataId) {
        TeamData teamData = new TeamData();
        if (teamDataId != -1) {
            teamData.setTeamDataId(teamDataId);
        }
        teamData.setTeamId(teamId).setInDate(new Date()).setUserId(userId).setDescription(description);
        boolean flag = teamDataService.saveOrUpdate(teamData);
        Assert.isTrue(flag, teamDataId == -1 ? "添加团队数据失败" : "更新团队数据失败");
        return Result.succeed(teamDataId == -1 ? "添加团队数据成功" : "更新团队数据成功", true);
    }

    /**
     * 管理员查询团队数据列表
     *
     * @param teamId 团队ID
     * @return Result<List < TeamData>>
     * @author CK
     * @date 2021/5/7 20:24
     */
    @GetMapping("/listTeamDataAdmin")
    public Result<List<TeamData>> listTeamDataAdmin(@RequestParam(value = "teamId") Integer teamId) {
        List<TeamData> list = teamDataService.listTeamDataAdmin(teamId);
        Assert.notNull(list, "查询团队数据失败");
        return Result.succeed("查询团队数据成功", list);
    }

    /**
     * 管理员删除团队数据
     *
     * @param teamDataId 团队数据ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/7 20:30
     */
    @PostMapping("/deleteTeamData")
    public Result<Boolean> deleteTeamData(@RequestParam(value = "teamDataId") Integer teamDataId) {
        boolean b = teamDataService.removeById(teamDataId);
        Assert.isTrue(b, "删除团队数据失败");
        return Result.succeed("删除团队数据成功", b);
    }

    /**
     * 管理员根据ID查询团队数据描述
     *
     * @param teamDataId 团队数据ID
     * @return Result<String>
     * @author CK
     * @date 2021/5/7 21:12
     */
    @GetMapping("/descriptionById")
    public Result<String> descriptionById(@RequestParam(value = "teamDataId") Integer teamDataId) {
        TeamData teamData = teamDataService.getById(teamDataId);
        Assert.notNull(teamData, "查询团队描述失败");
        return Result.succeed(ResultCode.SUCCESS.getCode(), "查询团队描述成功", teamData.getDescription());
    }

    /**
     * 用户查看团队信息
     *
     * @param teamId 团队ID
     * @return Result<TeamData>
     * @author CK
     * @date 2021/5/8 17:50
     */
    @GetMapping("/teamDataList")
    public Result<List<TeamData>> teamDataList(@RequestParam(value = "teamId") Integer teamId,
                                               @RequestParam(value = "email") String email) {
        Team team = teamService.getById(teamId);
        Assert.notNull(team, "无权访问");
        if (team.getPrivilege() == 1) {
            boolean flag = teamUserService.listValidate(teamId, email);
            Assert.isTrue(flag, "无权访问");
        }
        QueryWrapper<TeamData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id", teamId).orderByDesc("in_date");
        List<TeamData> list = teamDataService.list(queryWrapper);
        Assert.notNull(list, "出错了");
        return Result.succeed("查询团队数据成功", list);
    }

}

